package com.recharge.srevice;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Customer;
import com.entity.Meter;
import com.entity.Recharge;
import com.file.entity.FileCustomer;
import com.file.service.CustomerService;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.RechargeDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;

/**
 * @Title: CloseAccountService.java
 * @Package com.recharge.srevice
 * @author 孙梦楠
 * @date 2017年8月29日 上午8:29:02
 * @version V1.0.0
 */
@Service
public class CloseAccountService {

	@Autowired
	private RechargeDao rechargedao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CustomerService customerservice;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;
	@Autowired
	private MeasuringpointDao measuringpointdao;
	
	@Autowired
	private RechargeService rechargeservice;
	
	@Autowired
	private SysdicService sysdicservice;

	// 列出所有可以销户的用户
	@Transactional
	public JsonResult<List<FileCustomer>> listallcustomer(String opeorgcookie, String opetypecookie,String customerrechargestatus,
			String customeraccount, String customername, String customeraddress,String languagecookie,String childtotalstatus) {

		if(customerrechargestatus==null) {
			return customerservice.listallcustomer(opeorgcookie, opetypecookie, "2", "2", 571, "列出所有可销户,待销户，已销户用户", customeraccount,
					customername, customeraddress,languagecookie,childtotalstatus);
			
		}
		return customerservice.listallcustomer(opeorgcookie, opetypecookie, "2", customerrechargestatus, 571, "列出所有可销户,待销户，已销户用户", customeraccount,
				customername, customeraddress,languagecookie,childtotalstatus);
	}

	// 销户--先只改变用户开户状态
	@Transactional
	public JsonResult<Recharge> close_account(Integer customerid, String opecodecookie,String opetypecookie) {
		
		// 更改用户账户状态为待销户
		customerdao.updateCustomer_recharge("3", customerid);

		// 下发销户工单
		Recharge recharge = new Recharge();
		recharge.setCustomerid(customerid);
		Customer customer = customerdao.findOne(customerid);
		Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
		recharge.setMeterid(meterid);
		recharge.setCustomeraccount(customer.getCustomeraccount());
		recharge.setCustomername(customer.getCustomername());
		recharge.setCustomeraddress(customer.getCustomeraddress());
		recharge.setCustomerorg(customer.getCustomerorg());
		recharge.setCustomertype(opetypecookie);
		recharge.setDirection("待销户");
		recharge.setOpecode(opecodecookie);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String issuetime = sdf.format(new Date());
		recharge.setIssuetime(issuetime);
		recharge.setOptstatus("等待下发");
		recharge.setOptnum(0);

		rechargedao.save(recharge);

		return new JsonResult<Recharge>(572, "销户成功", null);

	}

	// 销户数据库操作
	@Transactional
	public void closeAccount_db(Integer  meterid) {

//		Meter meter = meterdao.findByMeteraddressAndMeterstatusNot(meteraddress, "4");
//		Integer meterid = meter.getMeterid();
		Integer customerid = customermeterdao.findByMeterid(meterid).getPk().getCustomerid();
		// 更改用户账户状态为已销户
		customerdao.updateCustomer_recharge("4", customerid);
		//删除关联关系
		customermeterdao.deleteByMeterid(meterid);
		//表状态改为待安装
		meterdao.updateMeter("1", meterid);
		//删除采集点
		measuringpointdao.deleteByMeterid(meterid);
		//更新工单为已销户
		rechargedao.updateDirection("已销户", "待销户", meterid);
		//更新remark，设置标志位，已销户
		rechargedao.updateRemark("已销户", meterid);
		
		
	}
	//更新工单
	@Transactional 
	public boolean updateRecharge(Recharge recharge) {
		List<Recharge> list = new ArrayList<Recharge>();
		list.add(recharge);
		
		return rechargeservice.updateRecharge(list);
	}

//	// 销户列表
//	@Transactional
//	public JsonResult<List<Recharge>> listclosing(String customeraccount, String customername, String customeraddress,
//			String customertype, Integer customerorg) {
//		
//		List<Recharge> recharges= rechargedao.findByCondition(customeraccount, customername,
//				customeraddress, customertype, customerorg , "销户");
//
//		return new JsonResult<List<Recharge>>(573, "销户历史",recharges);
//	}

}
